package com.swpu.demo.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.swpu.demo.entity.PersonalData;
import com.swpu.demo.service.PersonalDataService;
import com.swpu.demo.utils.JWTUtil;
import com.swpu.demo.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Author Name
 * @since 2021-05-12
 */
@RestController
@RequestMapping("/personalData")
@CrossOrigin
@Slf4j
@Api("系统管理员交互数据")
public class PersonalDataController {
    @Autowired
    PersonalDataService personalDataService;

    @GetMapping("/login")
    public Map<String, Object> login(PersonalData personalData, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        PersonalData User = personalDataService.selectOneUser(personalData.getUserName());
        if (User !=null && User.getPassWord().equals(MD5Util.code(personalData.getPassWord()))) {
            Map<String, String> payload = new HashMap<>();
            payload.put("userId", User.getUserId().toString());
            payload.put("userName", User.getUserName());
            String token = JWTUtil.getToken(payload);
            map.put("msg", "登陆成功!");
            map.put("token", token);
            session.setAttribute("personalData", User);
        } else {
            map.put("msg", "账户或密码错误!");
        }
        return map;
    }

    @PostMapping("/update")
    @ApiOperation(value = "更改信息")
    public Map<String, Object> update(HttpServletRequest request, PersonalData personalData) {
        //获取请求信息
        String token = request.getHeader("token");
        DecodedJWT verify = JWTUtil.verify(token);
        personalData.setUserId(Integer.valueOf(verify.getClaim("userId").asString()));
        personalData.setUserName(verify.getClaim("userName").asString());
        int i = personalDataService.updateOneUser(personalData);
        Map<String, Object> map = new HashMap<>();
        //处理自己业务逻辑
        if (i == 1) {
            map.put("msg", "更新成功！");
        } else {
            map.put("msg", "更新失败！");
        }
        return map;
    }

    @RequestMapping("/register")
    @ApiOperation(value = "注册用户")
    public Map<String, Object> insert( PersonalData personalData) {
        Map<String, Object> map = new HashMap<>();
        personalData.setPassWord(MD5Util.code(personalData.getPassWord()));//对密码进行md5加密，为方便未使用加盐
        int i = personalDataService.addOneUser(personalData);
        if (i == 1) {
            map.put("msg", "注册成功");
        } else {
            map.put("msg", "注册失败");
        }
        return map;
    }

    @RequestMapping("/logout")
    @ApiOperation("退出登录")
    public Map<String, Object> logOut(HttpServletRequest request,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        session.removeAttribute("personalData");
        map.put("msg", "退出成功");
        return map;
    }

}

